package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.RegisterMessage;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtils;

public class Register2Activity extends BaseActivity_NoBar {
	private EditText register_phone_number;
	public static String RandomCode = "RandomCode";
	public static String PhoneNumber = "PhoneNumber";
	
	private Handler handlerGetAuthNO = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					//{"status":false,"errorMsg":"用户已注册"}
					//{"message":{"isExist":false,"randomCode":"428593"},"status":true}
					com.alibaba.fastjson.JSONObject jsonObjectResult= JSON.parseObject(result);
					if("true".equals(jsonObjectResult.getString("status"))){
						RegisterMessage message = jsonObjectResult.getObject("message", RegisterMessage.class);
						Intent intent = new Intent(Register2Activity.this,Register2NextActivity.class);
						intent.putExtra(RandomCode,message.getRandomCode());
						intent.putExtra(PhoneNumber, register_phone_number.getText().toString().trim());
						startActivity(intent);
						startActivity_anim(0);
					}else if("false".equals(jsonObjectResult.getString("status"))){
						String emsg = jsonObjectResult.getString("errorMsg");
						if( emsg!= null && !emsg.equals("")) {
							showToast(emsg);
						} else {
							showToast("网络错误,请稍后再试!");
						}
					}
				} else {
					showToast("网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register2);
		register_phone_number = (EditText)findViewById(R.id.register_phone_number);
	}
	public void getAuthNO(View v){
		if(register_phone_number.getText().toString().trim().equals("")){
			showToast("请输入手机号注册");
		}else if(!StringUtils.isPhoneNum(register_phone_number.getText().toString().trim())){
			showToast("请输入正确的手机号注册");
		}else {
			String url = MainConstants.HOST_test+HttpConfigAPI.NEW_REGISTER;
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("mobileNumber",register_phone_number.getText().toString().trim()));
			new DataLoader(this).DisplayData(url , params , handlerGetAuthNO , "", false);
			showProgress("请稍等...");
		}
	}
	public void back(View v){
		finish();
	}
}
